package com.gg.zjj.service.impl;

import com.gg.common.mapper.*;
import com.gg.common.pojo.*;
import com.gg.zjj.mapper.DFileMApplyViewMapper;
import com.gg.zjj.pojo.DFileMApplyView;
import com.gg.zjj.pojo.MProcedureModuleView;
import com.gg.zjj.pojo.MProcedureView;
import com.gg.zjj.pojo.ProductionPlanNumberGroupVIew;
import com.gg.zjj.service.DevelopProductionDispatchOrdersService;
import com.gg.zjj.util.Result;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author koco
 * 2022/10/14
 * 14:43
 */
@Service
public class DevelopProductionDispatchOrdersServiceImpl implements DevelopProductionDispatchOrdersService {
    @Resource
    private DFileMApplyViewMapper viewMapper;
    @Resource
    private MDesignProcedureDetailsMapper detailsMapper;

    @Resource
    private MDesignProcedureModuleMapper moduleMapper;

    @Resource
    private MManufactureMapper manufactureMapper;

    @Resource
    private MProcedureMapper mProcedureMapper;

    @Resource
    private MProcedureModuleMapper procedureModuleMapper;

    @Resource
    private MApplyMapper applyMapper;

    @Override
    public List<DFileMApplyView> queryUndispatchedProductionSchedulesService() {
        return viewMapper.queryUndispatchedProductionSchedulesMapper();
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public Result<List<MDesignProcedureDetails>> checkProcessMaterialIsItDoneService(String v) {
        List<MDesignProcedureDetails> details = detailsMapper.checkProcessMaterialIsItDoneMapper(v);

        if (details.size() == 0) {
            return Result.error("该产品工序物料未设计完毕!!!");
        }
        int[] i = new int[details.size()];
        for (int j = 0; j < details.size(); j++) {
            i[j] = details.get(j).getId();
        }
        List<MDesignProcedureModule> moduleList = moduleMapper.queryByParentId(i);
        for (MDesignProcedureDetails detail : details) {
            List<MDesignProcedureModule> temp = new ArrayList<>();
            for (MDesignProcedureModule mDesignProcedureModule : moduleList) {
                if (detail.getId().equals(mDesignProcedureModule.getParentId())) {
                    temp.add(mDesignProcedureModule);
                }
            }
            detail.setProcessMaterialDetails(temp);
        }
        return Result.success(details);
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public Result submitProductionDispatchService(MManufacture o) {
        try {
            ProductionPlanNumberGroupVIew groupVIew=new ProductionPlanNumberGroupVIew();
            if(o.getApplyIdGroup().indexOf("|")>=0){
                String[] applyIdGroup = o.getApplyIdGroup().split("\\|");
                groupVIew.setApplyIdGroup(applyIdGroup);
            }else {
                String[] applyIdGroup = new String[1];
                applyIdGroup[0] = o.getApplyIdGroup();
                groupVIew.setApplyIdGroup(applyIdGroup);
            }
            groupVIew.setProductNumber(o.getProductId());
            applyMapper.modifyProductProductionPlanningStatus(groupVIew);
            String id = manufactureMapper.QueryTheLastDispatchOrderNumber();
            if (null == id) {
                o.setManufactureId(productionPlanNumber() + "0001");
            } else {
                String serialNumber = id.substring(id.length() - 4);
                serialNumber = String.valueOf(Integer.parseInt(serialNumber) + 1);
                while (serialNumber.length() < 4) {
                    serialNumber = "0" + serialNumber;
                }
                o.setManufactureId(productionPlanNumber() + serialNumber);
            }
            manufactureMapper.insertSelective(o);

            for (MProcedure procedure : o.getProductionProcesses()) {
                procedure.setParentId(o.getId());
                mProcedureMapper.insertSelective(procedure);
                for (MProcedureModule module : procedure.getProductionMaterial()) {
                    module.setParentId(procedure.getId());
                    procedureModuleMapper.insertSelective(module);
                }
            }
        } catch (Exception e) {
            return Result.error("派工错误");
        }
        return Result.success();
    }


    private String productionPlanNumber() {
        Date date = new Date();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String format1 = format.format(date);
        format1 = "300" + format1.replace("-", "");
        return format1;
    }
}
